/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apt.epj4.util;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author NGUYENVANHUNG
 */
public class Tools {

    //check date form
    public static boolean checkDateForm(String strDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        try {
            sdf.setLenient(false);
            sdf.parse(strDate);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //get Timestamp from date string
    public static Timestamp getTimestampFromString(String strDate) {
        Timestamp ts = null;
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        try {
            sdf.setLenient(false);
            Date d = sdf.parse(strDate);
            ts = new Timestamp(d.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return ts;
    }

    //get custom string from Timestamp
    public static String getStringFromTimestamp(Timestamp ts) {
        String s = null;
        if (ts != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            sdf.setLenient(false);
            s = sdf.format(new Date(ts.getTime()));
        }
        return s;
    }

    //example for BigDecimal
    public BigDecimal getFactorial(BigDecimal n) {
        BigDecimal res = BigDecimal.ONE;
        while (n.compareTo(BigDecimal.ONE) > 0) {
            res = res.multiply(n);
            n = n.subtract(BigDecimal.ONE);
        }
        return res;
    }

    //check number
    public static boolean checkIsNumber(String strNumber){
        try{
            Double.parseDouble(strNumber);
            return true;
        }
        catch(Exception ex){
            return false;
        }
    }

    //check email
    public static boolean checkEmail(String strEmail){
        if(!strEmail.matches("[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.[a-zA-Z0-9._-]+")){
            return false;
        }
        return true;
    }

    //check special character
    public static boolean checkSpecialCharacter(String str){
        if(!str.matches("[0-9a-zA-Z_ .]+")){
            return false;
        }
        return true;
    }

    //check phone number
    public static boolean checkPhone(String strPhone){
         if(!strPhone.matches("[0-9]{10,15}")){
            return false;
        }
        return true;
    }

}
